package com.protocols.sctp.m3ua;

import java.io.Serializable;
import java.lang.reflect.Array;
import java.nio.ByteBuffer;
import java.util.Arrays;

import com.protocols.*;

public class Status implements Serializable{
	private OctaveInteger ParameterTag= new OctaveInteger(13,2);
	private OctaveInteger ParameterLength= new OctaveInteger(8,2);
	private OctaveInteger StatusType= new OctaveInteger(2);
	private OctaveInteger StatusInfo= new OctaveInteger(2);
	private Boolean Visible=false;
	private Boolean Mandatory =true;
	

	
	public Status(Boolean ismandatory) {
		Mandatory=ismandatory;
	}

	public Status(int statustype, int statusinfo,Boolean mandatory) {		
		Mandatory=mandatory;		
		StatusType.Value = statustype;
		StatusInfo.Value = statusinfo;
		Visible=true;
	}
	
	public byte [] getByteArray(){		
		return  !Visible?new byte[0]: ProtoOperations.concatArrays(ParameterTag.getByteArray(), ParameterLength.getByteArray(), StatusType.getByteArray(), StatusInfo.getByteArray());

	}

	
	public int getParameterTag() {
		return ParameterTag.Value;
	}

	public int getLength(){
		return getByteArray().length;
	}

	public int getParameterLength() {
		return Visible?ParameterLength.Value:0;
	}




	public int getStatusType() {
		return StatusType.Value;
	}



	public int getStatusInfo() {
		return StatusInfo.Value;
	}


	public void setStatus(int statustype,int statusinfo) {
		StatusType.Value = statustype;
		StatusInfo.Value = statusinfo;
		Visible=true;
	}
	

	public byte [] decode(byte [] input) throws ProtocolStackException{
		
		if (input.length==0)
			return new byte[0];
		else if (input.length<8 && input.length>0)
			throw new ProtocolStackException("Decode Failure:ASPStatus"+ProtoOperations.getHexString(input));
		
		
		if (input[0]==0 && input[1]==13 && input[2]==0 && input[3]==8)
		{			
			ParameterLength.Value=ProtoOperations.intFromByteArray(input, 2, 2);
			setStatus( ProtoOperations.intFromByteArray(input, 4,  2),ProtoOperations.intFromByteArray(input, 6,  2));
			return ProtoOperations.extractByteArray(input, ParameterLength.Value, input.length-ParameterLength.Value);
		}
		else if (isMandatory())
			throw new ProtocolStackException("Decode Failure:ASPStatus"+ProtoOperations.getHexString(input));
		else
				return input;
					
		}

		public Boolean isMandatory() {
			return Mandatory;
		}

		public void setMandatory(Boolean mandatory) {
			Mandatory = mandatory;
		}
	
}
